<?php
/**
 * Created by PhpStorm.
 * User: 王飞
 * Date: 2017/9/6
 * Time: 21:33
 */
namespace app\index\controller;
use think\Controller;
use app\common\controller\WebFront;
use fast\third\Wechat;
use app\common\model\Userwechat;
use app\common\model\User;
use think\Exception;
use think\Session;
use app\common\model\Category;

class Know extends Controller
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index()
	{
	}
	
	/**
	 * @return \think\response\Json
	 * 甄知识分分类
	 */
	public function knowclass(){
		$model=new Category();
		$pdata=$model
			->order("weigh")
			->where('pid',0)
			->select();
		if(!$pdata){
			$result='{ "error": errorcode , }';
			return json($result);
		}
		$data=[];
		foreach ($pdata as $key =>$value){
			$item['id']=$value['id'];
			$item['name']=$value['name'];
			$item['child']=db('category')->order('weigh')->where('pid',$value['id'])->field('id,name')->select();
			array_push($data,$item);
		}
		$result = array("success"=>1,"data" => $data);
		return json($result);
	}
	
	/**
	 * 获取列表
	 */
	public function knowlist(){
		$category=isset($_GET['category'])?$_GET['category']:21;
		$size=isset($_GET['size'])?$_GET['size']:10;
		$pagenum=isset($_GET['pagenum'])?$_GET['pagenum']:1;
		$total = db('page')
			->order('weigh')
			->count();
		$data=db('page')
			->where('category_id',$category)
			->field('id,title,summary,image')
			->limit($size)
			->order('weigh')
			->page($pagenum)
			->select();
		$result = array("success"=>1,"total"=>$total,"data" => $data);
		return json($result);
	}
	
	/**
	 * 获取列表
	 */
	public function knowcontext($id=null){
		if(!isset($id)){
			$result = array("error"=>1,"data" => "no id");
			return json($result);
		}
		
		$data=db('page')
			->where('id',$id)
			->field('id,title,content,summary,createtime')
			->find();
		$result = array("success"=>1,"data" => $data);
		return json($result);
	}
}